//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartLegendView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartLegendView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

class CMyChart : public CBCGPChartVisualObject
{
	DECLARE_DYNCREATE(CMyChart)

public:
	BOOL m_bOwnerDrawLegendKey;

public:
	virtual BOOL IsLegendHorizontal() const 
	{
		if (IsLegendCustomPosition())
		{
			// legend at custom position is vertical
			return FALSE;
		}
		
		return CBCGPChartVisualObject::IsLegendHorizontal();
	}
	
	void EnableLegendCustomPosition(BOOL bEnable, BOOL bLegendOverlapsChart = FALSE)
	{
		m_ptLegendCustomPos = bEnable ? CBCGPPoint(0, 25) : CBCGPPoint(-1, -1);
		m_chartLayout.m_bLegendOverlapsChart = bLegendOverlapsChart;
		m_rectPlotAreaPadding.left = 0;
	}
	
	BOOL IsLegendCustomPosition() const
	{
		return m_ptLegendCustomPos != CBCGPPoint(-1, -1);
	}

protected:
	CMyChart()
	{
		m_bOwnerDrawLegendKey = FALSE;
		m_brWhite.SetColor(CBCGPColor::White, .7);
		m_ptLegendCustomPos.SetPoint(-1, -1);
	}

	virtual BOOL OnCalcDataLabelSize(CBCGPGraphicsManager* pGM, const CString& strText, CBCGPChartSeries* pSeries, int nDataPointIndex, CBCGPSize& sz)
	{
		if (m_bOwnerDrawLegendKey)
		{
			return CBCGPChartVisualObject::OnCalcDataLabelSize(pGM, strText, pSeries, nDataPointIndex, sz);
		}
		sz = GetScaleRatio() * 25.;
		return TRUE; 
	}

	virtual void OnDrawChartSeriesDataLabel(CBCGPGraphicsManager* pGM, const CBCGPRect& rectText, const CString& strText, CBCGPChartSeries* pSeries, int nDataPointIndex)
	{
		if (m_bOwnerDrawLegendKey)
		{
			CBCGPChartVisualObject::OnDrawChartSeriesDataLabel(pGM, rectText, strText, pSeries, nDataPointIndex);
		}
		else
		{
			BCGPSeriesColorsPtr colors;
			const BCGPChartFormatSeries* pFormatSeries = pSeries->GetColors(colors, nDataPointIndex);
			if (pFormatSeries == NULL)
			{
				return;
			}

			const BCGPChartFormatLine& lineFormat = pFormatSeries->m_dataLabelFormat.m_outlineFormat;

			CBCGPPoint ptLabelLineStart;
			CBCGPPoint ptLabelLineEnd;
			CBCGPPoint ptLabelUnderline;
			pSeries->GetDataPointLabelDropLines(nDataPointIndex, ptLabelLineStart, ptLabelLineEnd, ptLabelUnderline);

			pGM->DrawLine(ptLabelLineStart, ptLabelLineEnd, *colors.m_pBrDataLabelLineColor,  
				lineFormat.m_dblWidth, &lineFormat.m_strokeStyle);

			m_bOwnerDrawLegendKey = TRUE;
			OnDrawChartLegendKey(pGM, rectText, pSeries->GetDataPointFormat(nDataPointIndex, FALSE), pSeries, nDataPointIndex);
			m_bOwnerDrawLegendKey = FALSE;
		}
	}

	virtual void OnDrawChartLegendKey(
		CBCGPGraphicsManager* pGM, const CBCGPRect& rectLegendKey, 
		const BCGPChartFormatSeries* pSeriesStyle, CBCGPChartSeries* pSeries, int nDataPointIndex)
	{
		ASSERT_VALID(pGM);

		if (!m_bOwnerDrawLegendKey)
		{
			CBCGPChartVisualObject::OnDrawChartLegendKey(
				pGM, rectLegendKey, pSeriesStyle, pSeries, nDataPointIndex);
			return;
		}

		BCGPSeriesColorsPtr colors;
		const BCGPChartFormatSeries* pFormatSeries = pSeries->GetColors(colors, nDataPointIndex);
		if (pFormatSeries == NULL)
		{
			return;
		}

		const int count = 7;
		CBCGPPointsArray arPoints;
		arPoints.SetSize(count);

		if (pGM->GetType () == CBCGPGraphicsManager::BCGP_GRAPHICS_MANAGER_GDI)
		{
			arPoints[0] = CBCGPPoint( 4.0, 24.0);
			arPoints[1] = CBCGPPoint( 4.0, 11.0);
			arPoints[2] = CBCGPPoint( 0.0, 11.0);
			arPoints[3] = CBCGPPoint(12.0,  0.0);
			arPoints[4] = CBCGPPoint(24.0, 11.0);
			arPoints[5] = CBCGPPoint(20.0, 11.0);
			arPoints[6] = CBCGPPoint(20.0, 24.0);
		}
		else
		{
			arPoints[0] = CBCGPPoint( 4.5, 24.5);
			arPoints[1] = CBCGPPoint( 4.5, 11.5);
			arPoints[2] = CBCGPPoint( 0.0, 11.5);
			arPoints[3] = CBCGPPoint(12.5,  0.0);
			arPoints[4] = CBCGPPoint(24.5, 11.5);
			arPoints[5] = CBCGPPoint(20.5, 11.5);
			arPoints[6] = CBCGPPoint(20.5, 24.5);
		}

		CBCGPPoint ptOffset(rectLegendKey.TopLeft ());
		arPoints.Scale(CBCGPPoint(), GetScaleRatio().cx, GetScaleRatio().cy);
		arPoints += ptOffset;

		CBCGPPolygonGeometry geometry(arPoints);

		CBCGPBrush* pBrFill = colors.m_pBrElementFillColor;
		CBCGPBrush* pBrLine = colors.m_pBrElementLineColor;

		pGM->FillGeometry(geometry, *pBrFill);
		pGM->DrawGeometry(geometry, *pBrLine, (GetScaleRatio().cx + GetScaleRatio().cy) / 2.0);

		CBCGPRect rect(9, 14, 16, 21);
		rect.Scale(GetScaleRatio().cx, GetScaleRatio().cy, CBCGPPoint());
		rect += ptOffset;

		pGM->FillRectangle(rect, m_brWhite);
		pGM->DrawRectangle(rect, *pBrLine, (GetScaleRatio().cx + GetScaleRatio().cy) / 2.0);
	}

	virtual CBCGPSize OnCalcLegendKeySize(CBCGPChartSeries* pSeries, int nDataPointIndex)
	{
		if (!m_bOwnerDrawLegendKey)
		{
			return CBCGPChartVisualObject::OnCalcLegendKeySize(pSeries, nDataPointIndex);
		}

		return GetScaleRatio() * 25.;
	}

	virtual BOOL OnGetLegendAreaRect(CBCGPGraphicsManager* pGM, CBCGPRect& rectLegendArea) 
	{
		if (!IsLegendCustomPosition())
		{
			return FALSE;
		}

		CBCGPSize szLegend = CalcLegendSize(pGM);
		rectLegendArea.SetRect(m_ptLegendCustomPos, szLegend);

		m_rectPlotAreaPadding.left = m_chartLayout.m_bLegendOverlapsChart ? 0 : rectLegendArea.right;
		return TRUE;
	}

protected:
	CBCGPBrush	m_brWhite;
	CBCGPPoint	m_ptLegendCustomPos;
};

IMPLEMENT_DYNCREATE(CMyChart, CBCGPChartVisualObject)

/////////////////////////////////////////////////////////////////////////////
// CChartLegendView

IMPLEMENT_DYNCREATE(CChartLegendView, CBCGPChartExampleView)

CChartLegendView::CChartLegendView()
	: CBCGPChartExampleView(CChartLegendView::IDD)
{
	//{{AFX_DATA_INIT(CChartLegendView)
	m_bLegendOverlapsChart = FALSE;
	m_nLegendPosition = 3;
	m_bShowLegendShape = TRUE;
	m_bOwnerDraw = FALSE;
	//}}AFX_DATA_INIT

	m_wndChart.CreateCustomChart(RUNTIME_CLASS(CMyChart));
}

CChartLegendView::~CChartLegendView()
{
}

void CChartLegendView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartLegendView)
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_CHART_LEGEND_OVERLAPS_CHART, m_bLegendOverlapsChart);
	DDX_CBIndex(pDX, IDC_LEGEND_POS, m_nLegendPosition);
	DDX_Check(pDX, IDC_CHART_LEGEND_SHAPE, m_bShowLegendShape);
	DDX_Check(pDX, IDC_OWNER_DRAW_LEGEND, m_bOwnerDraw);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartLegendView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartLegendView)
	ON_BN_CLICKED(IDC_CHART_LEGEND, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_LEGEND_OVERLAPS_CHART, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_LEGEND_POS, OnUpdateChart)
	ON_BN_CLICKED(IDC_CHART_LEGEND_SHAPE, OnUpdateChart)
	ON_BN_CLICKED(IDC_OWNER_DRAW_LEGEND, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartLegendView diagnostics

#ifdef _DEBUG
void CChartLegendView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartLegendView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartLegendView message handlers

void CChartLegendView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartType(BCGPChartColumn);

	pChart->AddChartData(_T("Item 1"), 2.);
	pChart->AddChartData(_T("Item 2"), 4.);
	pChart->AddChartData(_T("Item 3"), 5.);
	pChart->AddChartData(_T("Item 4"), 3.);
	pChart->AddChartData(_T("Item 5"), 7.);
	pChart->AddChartData(_T("Item 6"), 6.);

	CBCGPChartSeries* pSeries = pChart->GetSeries(0);
	ASSERT_VALID(pSeries);

	pSeries->EnableAutoColorDataPoints();

	OnUpdateChart();
	
	pChart->ShowDataLabels(TRUE);
	pChart->SetSeriesShadow();

	pChart->SetThemeOpacity(60);
	pChart->Redraw();
}

void CChartLegendView::OnUpdateChart() 
{
	UpdateData();

	CMyChart* pChart = DYNAMIC_DOWNCAST(CMyChart, m_wndChart.GetChart());
	ASSERT_VALID(pChart);

	if (m_nLegendPosition == 5)
	{
		pChart->EnableLegendCustomPosition(TRUE, m_bLegendOverlapsChart);
	}
	else
	{
		pChart->EnableLegendCustomPosition(FALSE); // clear custom state
		pChart->SetLegendPosition((BCGPChartLayout::LegendPosition)(m_nLegendPosition + 1), m_bLegendOverlapsChart);
	}
	
	pChart->EnableDrawLegendShape(m_bShowLegendShape);

	pChart->m_bOwnerDrawLegendKey = m_bOwnerDraw;

	pChart->Redraw();
}
